package dao;

import entity.Orgnazition;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

public class InstitutionalDao extends BaseDao{
    Orgnazition orgnazition = null;
    public List<Orgnazition> info(){
        List<Orgnazition> list = new ArrayList<>();
        try{
            conn = super.getConnection();
            String sql  = "select * from orgnazition";
            pstmt = conn.prepareStatement(sql);
            rs = pstmt.executeQuery();
            while (rs.next()) {
                orgnazition = new Orgnazition();
                orgnazition.setId(rs.getInt("id"));
                orgnazition.setName(rs.getString("name"));
                orgnazition.setPrincipalName(rs.getString("principalName"));
                orgnazition.setStatus(rs.getInt("status"));
                orgnazition.setCreateDate(rs.getString("createDate"));
                list.add(orgnazition);
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }finally {
            closeAll(conn,pstmt,rs);
        }
        return list;
    }

    public int add(Orgnazition orgnazition){
        int i = 0;
        try{
            conn = super.getConnection();
            String sql = "INSERT INTO `orgnazition`( `name`, `principalName`,`createDate`) VALUES (?,?,?);";
            pstmt = conn.prepareStatement(sql);
            pstmt.setString(1,orgnazition.getName());
            pstmt.setString(2,orgnazition.getPrincipalName());
            pstmt.setString(3,orgnazition.getCreateDate());
            i = pstmt.executeUpdate();
        } catch (SQLException e) {
            e.printStackTrace();
        }finally {
            closeAll(conn,pstmt,rs);
        }
        return i;
    }

    public boolean name(String name){
        boolean whether = false;

        try {
            conn = super.getConnection();
            String sql = "select * from orgnazition where name = ?";
            pstmt = conn.prepareStatement(sql);
            pstmt.setString(1,name);
            rs = pstmt.executeQuery();
            if (rs.next()){
                whether = true;
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }finally {
            closeAll(conn,pstmt,rs);
        }
        return whether;
    }

    public int update(int status,int id){
        int i = 0;
        try{
            conn = super.getConnection();
            String sql = "UPDATE  `orgnazition` SET  `status` =?  WHERE `id` = ?;";
            pstmt = conn.prepareStatement(sql);
            pstmt.setInt(1,status);
            pstmt.setInt(2,id);
            i = pstmt.executeUpdate();

        } catch (SQLException e) {
            e.printStackTrace();

        }finally {
            closeAll(conn,pstmt,rs);
        }
        return i;
    }
}
